assert(DongleStub, string.format("clsaver requires DongleStub."))
assert(DongleStub:GetVersion() == "DongleStub-Beta0", 
       string.format("clsaver requires DongleStub-Beta0.  You are using an older version."))
assert(DongleStub("Dongle-Beta0"), 
       string.format("PerfectRaid requires Dongle-Beta0.  You are using an older version."))

local clsaver = DongleStub("Dongle-Beta0"):New("clsaver")

-------------------------------------------------------------------------------
-- DB defaults
-------------------------------------------------------------------------------
clsaver_DB = {
   zones = { },
}

function clsaver:Initialize()
   self.defaults = {
      profile = {
	 zones = {}
      }
   }
end

function clsaver:Enable()
   self.db = self:InitializeDB("clsaver_DB", self.defaults)

   self.cmds = self:InitializeSlashCommand("CombatLog Saver - v1.3", "CLSAVER", "clsaver")
   self.cmds:RegisterSlashHandler("list: Lists zones for which CombatLog is turned on", "^list$", "ListZones")
   self.cmds:RegisterSlashHandler("toggle: Toggles CombatLog on/off for the current zone", "^toggle$", "ToggleTracking")

   self:RegisterEvent("ZONE_CHANGED_NEW_AREA", "Reconfigure")
   self:RegisterEvent("PLAYER_ENTERING_WORLD", "Reconfigure")
end

function clsaver:IsZoneTracked()
   return self.db.profile.zones[self.current_zone] and true or false
end

function clsaver:ToggleTracking()
   if self.db.profile.zones[self.current_zone] then
      self.db.profile.zones[self.current_zone] = nil
   else
      self.db.profile.zones[self.current_zone] = true
   end
   self:Reconfigure()
   if self.db.profile.zones[self.current_zone] then
      self:PrintF("CombatLog on for " .. self.current_zone)
   else
      self:PrintF("CombatLog off for " .. self.current_zone)
   end
end

function clsaver:ListZones()
   if next(self.db.profile.zones) then
      self:PrintF("CombatLog on in zones:")
      for z,s in pairs(self.db.profile.zones) do
	 self:PrintF("* " .. z)
      end
   else
      self:PrintF("CombatLog is off for all zones")
   end
end

local RANGE_VARS = {
   "CombatLogRangeCreature",
   "CombatLogRangeParty",
   "CombatLogRangePartyPet",
   "CombatLogRangeFriendlyPlayers",
   "CombatLogRangeFriendlyPlayersPets",
   "CombatLogRangeHostilePlayers",
   "CombatLogRangeHostilePlayersPets"
}

function clsaver:StartLogging()
   assert(not LoggingCombat())
   for _,var in ipairs(RANGE_VARS) do
      SetCVar(var, 100)
   end
   LoggingCombat(true)
end

function clsaver:StopLogging()
   assert(LoggingCombat())
   for _,var in ipairs(RANGE_VARS) do
      SetCVar(var, GetCVarDefault(var))
   end
   LoggingCombat(false)
end

function clsaver:Reconfigure()
   self.current_zone = GetRealZoneText()
   if (self:IsZoneTracked() and not LoggingCombat()) then
      UIErrorsFrame:AddMessage("CombatLog ON", 0.2, 1.0, 0.2, 1.0, UIERRORS_HOLD_TIME)
      self:StartLogging()
   elseif (not self:IsZoneTracked() and LoggingCombat()) then
      UIErrorsFrame:AddMessage("CombatLog OFF", 1.0, 0.0, 0.0, 1.0, UIERRORS_HOLD_TIME)
      self:StopLogging()
   end
end
